find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_FOUND})
    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK    ${CCACHE_FOUND})
endif(CCACHE_FOUND)

project(minc-toolkit-v2 )

enable_language(C)
enable_language(CXX)
#enable_language(Fortran)

#Turn on PIC (and PIE if supported)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

cmake_minimum_required(VERSION 3.6.1)

IF(COMMAND CMAKE_POLICY)
  IF(POLICY CMP0026)
      CMAKE_POLICY(SET CMP0026 NEW)
  ENDIF(POLICY CMP0026)
  IF(POLICY CMP0046)
      CMAKE_POLICY(SET CMP0046 OLD)
  ENDIF(POLICY CMP0046)
  if(POLICY CMP0083)
    cmake_policy(SET CMP0083 NEW)
  endif()
ENDIF(COMMAND CMAKE_POLICY)



SET(MINC_TOOLKIT_PACKAGE_VERSION_MAJOR 1)
SET(MINC_TOOLKIT_PACKAGE_VERSION_MINOR 9)
SET(MINC_TOOLKIT_PACKAGE_VERSION_PATCH 18)
SET(MINC_TOOLKIT_RELEASE_DATE "20201015")

SET(MINC_TOOLKIT_VERSION_FULL "${MINC_TOOLKIT_PACKAGE_VERSION_MAJOR}.${MINC_TOOLKIT_PACKAGE_VERSION_MINOR}.${MINC_TOOLKIT_PACKAGE_VERSION_PATCH}-${MINC_TOOLKIT_RELEASE_DATE}")

#SET(CPACK_GENERATOR DEB)
SET(CPACK_PACKAGE_NAME "minc-toolkit-v2")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "minc image processing toolkit")
SET(CPACK_PACKAGE_VENDOR "Brain Imaging Center, Montreal Neurological Institute, McGill University")
SET(CPACK_PACKAGE_VERSION_MAJOR ${MINC_TOOLKIT_PACKAGE_VERSION_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${MINC_TOOLKIT_PACKAGE_VERSION_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${MINC_TOOLKIT_PACKAGE_VERSION_PATCH})
SET(CPACK_PACKAGE_CONTACT "Vladimir Fonov <vladmir.fonov@gmail.com>")
SET(CPACK_PACKAGE_INSTALL_DIRECTORY "minc-toolkit-${MINC_TOOLKIT_VERSION_FULL}")
SET(CPACK_MONOLITHIC_INSTALL ON)
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README.md")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING.txt")
SET(CPACK_PACKAGE_RELOCATABLE OFF)

#IF (CMAKE_SYSTEM_PROCESSOR MATCHES "unknown")
EXEC_PROGRAM(uname ARGS "-m" OUTPUT_VARIABLE CMAKE_SYSTEM_PROCESSOR)
#ENDIF (CMAKE_SYSTEM_PROCESSOR MATCHES "unknown")

IF(NOT DEFINED CPACK_SYSTEM_NAME)
  SET(MT_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR})
ENDIF(NOT DEFINED CPACK_SYSTEM_NAME)

IF(${CMAKE_SYSTEM_NAME} MATCHES Windows)
  IF(CMAKE_CL_64)
    SET(MT_SYSTEM_NAME win64-${CMAKE_SYSTEM_PROCESSOR})
  ELSE(CMAKE_CL_64)
    SET(MT_SYSTEM_NAME win32-${CMAKE_SYSTEM_PROCESSOR})
  ENDIF(CMAKE_CL_64)
ELSE(${CMAKE_SYSTEM_NAME} MATCHES Windows)
  IF(${CMAKE_SYSTEM_NAME} MATCHES Linux )
    EXEC_PROGRAM(lsb_release ARGS "-s -i" OUTPUT_VARIABLE SYSTEM_ID)
    EXEC_PROGRAM(lsb_release ARGS "-s -r" OUTPUT_VARIABLE SYSTEM_RELEASE)
    SET(MT_SYSTEM_NAME "${SYSTEM_ID}_${SYSTEM_RELEASE}-${CMAKE_SYSTEM_PROCESSOR}")
  ENDIF(${CMAKE_SYSTEM_NAME} MATCHES Linux )
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES Windows)

IF( NOT CMAKE_BUILD_TYPE )
   message("Build type is not set, setting to Release!")
   SET( CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE )
ENDIF()

SET(CPACK_PACKAGE_FILE_NAME "minc-toolkit-${MINC_TOOLKIT_VERSION_FULL}-${MT_SYSTEM_NAME}")

# register local modules
SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_SOURCE_DIR}/cmake-modules")

INCLUDE(PreventInSourceBuilds)
AssureOutOfSourceBuilds(minc-toolkit-v2)
# a hack to disable internal man pages per project
SET(DISABLE_MAN_PAGES_INSTALL ON)
INCLUDE(InstallManPages)
INCLUDE(DownloadPackage)

# default install directories
SET(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH "Install path prefix, prepended onto install directories.")
SET(CPACK_SET_DESTDIR    ON)
SET(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
SET(CPACK_STRIP_FILES    ON)

# MAC OS X magick
SET(MACOSX_RPATH  ON)
SET(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
SET(CMAKE_SKIP_RPATH OFF)
SET(CMAKE_SKIP_INSTALL_RPATH OFF)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)

# superbuild options
OPTION(MT_BUILD_LITE         "Build lite version of minc-toolkit, without ITK or visual tools" OFF)
OPTION(MT_BUILD_SHARED_LIBS  "Build shared libraries." OFF)
OPTION(USE_SYSTEM_ZLIB       "Use System ZLIB "       OFF)
#OPTION(USE_SYSTEM_NIFTI      "Use System NIFTI "       OFF)
#OPTION(USE_SYSTEM_GIFTI      "Use System GIfTI"       OFF)
OPTION(USE_SYSTEM_NETCDF     "Use System NETCDF > 3.6.3" OFF)
OPTION(USE_SYSTEM_HDF5       "Use System HDF5 > 1.8"     OFF)
OPTION(USE_SYSTEM_PCRE       "Use System PCRE "        OFF)
OPTION(USE_SYSTEM_GSL        "Use System GSL "         OFF)
OPTION(USE_SYSTEM_FFTW3F     "Use System FFTW3F "      OFF)
OPTION(USE_SYSTEM_FFTW3D     "Use System FFTW3D "      OFF)
OPTION(USE_SYSTEM_GLUT       "Use System GLUT  "       ON)
OPTION(USE_SYSTEM_OPENJPEG   "Use System OpenJPEG  "   OFF)
OPTION(USE_SYSTEM_JPEG       "Use System JPEG  "   OFF)

OPTION(MT_BUILD_ITK_TOOLS    "Build ITK-based tools"  ON)
OPTION(MT_BUILD_VISUAL_TOOLS "Build visual tools (Display, Register, postf, ray_trace) requires OpenGL & Glut"  OFF)

OPTION(USE_SYSTEM_ITK        "Use System ITK" OFF)
OPTION(MT_USE_OPENMP         "Use OpenMP multi-threading" OFF)
OPTION(MT_BUILD_IM           "Build ImageMagick and dependent libraries" OFF)
OPTION(MT_BUILD_OPENBLAS     "Build OpenBLAS: will require fortran" OFF)
#OPTION(MT_USE_OPENMP_BLAS    "Use Build OpenBLAS with OpenMP support" OFF) # TODO: finish this to compile on some platforms

SET(MT_PACKAGES_PATH "${CMAKE_BINARY_DIR}/cache" CACHE PATH  "Directory containing 3rd party packages")

IF(MT_BUILD_LITE)
  SET(MT_BUILD_ITK_TOOLS    OFF)
  SET(MT_BUILD_VISUAL_TOOLS OFF)
  SET(MT_BUILD_ANTS         OFF)
  SET(MT_BUILD_C3D          OFF)
  SET(MT_BUILD_ELASTIX      OFF)

  SET(CPACK_PACKAGE_FILE_NAME "minc-toolkit-${MINC_TOOLKIT_VERSION_FULL}-${MT_SYSTEM_NAME}-lite")
ENDIF(MT_BUILD_LITE)



ENABLE_TESTING()
INCLUDE(CTest)

# provide test environement
# CACHE INTERNAL FORCE
# for superbuild
SET(USE_SUPERBUILD ON)
INCLUDE(ExternalProject)
INCLUDE(ConfigureExternalProjects)
SET(SUPERBUILD_STAGING_PREFIX ${CMAKE_BINARY_DIR}/external)

# Fix missing share directory
file(MAKE_DIRECTORY ${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/share)

SET(MINC_TEST_ENVIRONMENT
 "PATH=${CMAKE_CURRENT_BINARY_DIR}:${CMAKE_CURRENT_BINARY_DIR}/N3:${CMAKE_CURRENT_BINARY_DIR}/classify:${CMAKE_CURRENT_BINARY_DIR}/minctools/progs:${CMAKE_CURRENT_BINARY_DIR}/mni_autoreg/mincblur:${CMAKE_CURRENT_BINARY_DIR}/mni_autoreg/make_phantom:${CMAKE_CURRENT_BINARY_DIR}/mni_autoreg/minctracc:${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/bin:$ENV{PATH}" 
)

LIST(APPEND MINC_TEST_ENVIRONMENT "PERL5LIB=${CMAKE_SOURCE_DIR}/mni-perllib:${CMAKE_CURRENT_BINARY_DIR}/mni-perllib/perl:${CMAKE_CURRENT_BINARY_DIR}/perl-modules:$ENV{PERL5LIB}")


IF(APPLE)
  LIST(APPEND MINC_TEST_ENVIRONMENT "DYLD_LIBRARY_PATH=${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}:${CMAKE_CURRENT_BINARY_DIR}/libminc:${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}:${CMAKE_CURRENT_BINARY_DIR}/libminc/ezminc:$ENV{DYLD_LIBRARY_PATH}")
ELSE(APLLE)
  LIST(APPEND MINC_TEST_ENVIRONMENT "LD_LIBRARY_PATH=${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}:${CMAKE_CURRENT_BINARY_DIR}/libminc:${CMAKE_CURRENT_BINARY_DIR}/libminc/ezminc:$ENV{LD_LIBRARY_PATH}")
ENDIF(APPLE)


# required packages
FIND_PACKAGE( Perl   REQUIRED )

# required for minc
IF(USE_SYSTEM_ZLIB)
  FIND_PACKAGE( ZLIB   REQUIRED )
ELSE()
  INCLUDE(BuildZLIB)
  build_zlib( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
  # we should have ZLIB at this point
  SET(HAVE_ZLIB ON)
ENDIF()


IF(USE_SYSTEM_NETCDF)
  FIND_PACKAGE( NETCDF REQUIRED )
ELSE()
  INCLUDE(BuildNETCDF)
  build_netcdf( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF()


IF(USE_SYSTEM_HDF5)
  IF(MT_BUILD_ITK_TOOLS)
    # need to find more
    FIND_PACKAGE(HDF5 REQUIRED COMPONENTS C CXX HL)
  ELSE()
    FIND_PACKAGE(HDF5 REQUIRED )
  ENDIF()

  #HACK to make FIND_PACKAGE adn build_hdf5 compatible
  IF(NOT HDF5_LIBRARIES AND HDF5_LIBRARY)
    SET(HDF5_LIBRARIES    ${HDF5_LIBRARY})
  ENDIF()

ELSE()
  INCLUDE(BuildHDF5)
  build_hdf5( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
  IF(NOT USE_SYSTEM_ZLIB)
    add_dependencies(HDF5 ZLIB)
  ENDIF()
ENDIF()

IF(USE_SYSTEM_NIFTI)
  FIND_PACKAGE(NIFTI REQUIRED)
ELSE()
  INCLUDE(BuildNIFTI)
  build_nifti( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
  IF(NOT USE_SYSTEM_ZLIB)
    add_dependencies(NIFTI ZLIB)
  ENDIF()
ENDIF()

# IF(USE_SYSTEM_GIFTI)
#   FIND_PACKAGE( GIFTI OPTIONAL )
# ELSE(USE_SYSTEM_GIFTI)
#   INCLUDE(BuildGIFTI)
#   build_gifti( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
#   IF(NOT USE_SYSTEM_NIFTI)
#     add_dependencies(GIFTI NIFTI)
#   ENDIF(NOT USE_SYSTEM_NIFTI)
# ENDIF(USE_SYSTEM_GIFTI)

# for minctools (dcm2mnc)
IF(USE_SYSTEM_OPENJPEG)
  FIND_PACKAGE( OpenJPEG )
ELSE()
  INCLUDE(BuildOpenJPEG)
  build_openjpeg( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF()

IF(USE_SYSTEM_JPEG)
  FIND_PACKAGE( JPEG )
ELSE()
  INCLUDE(BuildJPEG)
  build_jpeg( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF()

#required for some minc tools
FIND_PACKAGE( BISON )
FIND_PACKAGE( FLEX )
FIND_PACKAGE( Threads )

#required for bicpl
IF( USE_SYSTEM_NETPBM ) # else we will use bundled libppm
  FIND_PACKAGE( NETPBM QUIET )
ENDIF( USE_SYSTEM_NETPBM )

#required for oobicpl
IF(USE_SYSTEM_PCRE)
  FIND_PACKAGE( PCRE QUIET )
ELSE()
  INCLUDE(BuildPCRE)
  build_pcre(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF()

# required for mni_autoreg
INCLUDE(BuildLIBLBFGS)
build_liblbfgs(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})

SET(MINC_TOOLKIT_BUILD ON)
SET(MINC_TOOLKIT_BUILD_DIR   ${CMAKE_BINARY_DIR})
SET(MINC_TOOLKIT_INSTALL_DIR ${CMAKE_INSTALL_PREFIX})

#setup parameters
SET(LIBMINC_BUILD_V2           ON)
SET(LIBMINC_MINC1_SUPPORT      ON)
SET(LIBMINC_NIFTI_SUPPORT      OFF)
SET(LIBMINC_FOUND              ON)
SET(LIBMINC_BUILD_TOOLS        ON)
SET(LIBMINC_BUILD_CONVERTERS   ON)
SET(LIBMINC_BUILD_EZMINC       ON)
SET(LIBMINC_BUILD_EZMINC_EXAMPLES ON)
SET(LIBMINC_INSTALL_BIN_DIR bin)
SET(LIBMINC_INSTALL_LIB_DIR lib${LIB_SUFFIX})
SET(LIBMINC_INSTALL_INCLUDE_DIR include)
SET(LIBMINC_INSTALL_DATA_DIR share)
SET(LIBMINC_INTEGRATE_VOLUME_IO OFF)
SET(LIBMINC_EXPORTED_TARGETS "LIBMINC-targets")
# we have both MINC1 API
SET(HAVE_MINC1 ON)
# and MINC2 API
SET(HAVE_MINC2 ON)

IF(MT_BUILD_SHARED_LIBS)
  SET(LIBMINC_BUILD_SHARED_LIBS    ON)
  #EBTKS
  SET(EBTKS_BUILD_SHARED_LIBS      ON)
  #bicpl
  SET(BICPL_BUILD_SHARED_LIBS      ON)
  #oobicpl
  SET(OOBICPL_BUILD_SHARED_LIBS    ON)
  #arguments
  SET(ARGUMENTS_BUILD_SHARED_LIBS  ON)
  #bicgl
  SET(BICGL_BUILD_SHARED_LIBS      OFF)
ELSE(MT_BUILD_SHARED_LIBS)
  SET(LIBMINC_BUILD_SHARED_LIBS    OFF)
  #EBTKS
  SET(EBTKS_BUILD_SHARED_LIBS      OFF)
  #bicpl
  SET(BICPL_BUILD_SHARED_LIBS      OFF)
  #oobicpl
  SET(OOBICPL_BUILD_SHARED_LIBS    OFF)
  #arguments
  SET(ARGUMENTS_BUILD_SHARED_LIBS  OFF)
  #bicgl
  SET(BICGL_BUILD_SHARED_LIBS      OFF)
ENDIF(MT_BUILD_SHARED_LIBS)


#EZMINC
SET(EZMINC_BUILD_MINCNLM  ON)
SET(EZMINC_BUILD_DISTORTION_CORRECTION  ON)
SET(EZMINC_BUILD_MRFSEG  ON)
SET(EZMINC_BUILD_DD      ON)
SET(EZMINC_BUILD_TOOLS   ON)

ADD_SUBDIRECTORY(libminc)

SET(MINC2_deps "")

IF(NOT USE_SYSTEM_NETCDF)
  SET(MINC2_deps ${MINC2_deps} NETCDF)
ENDIF(NOT USE_SYSTEM_NETCDF)

IF(NOT USE_SYSTEM_HDF5)
  SET(MINC2_deps ${MINC2_deps} HDF5)
ENDIF(NOT USE_SYSTEM_HDF5)

IF(NOT USE_SYSTEM_NIFTI)
  SET(MINC2_deps ${MINC2_deps} NIFTI)
ENDIF(NOT USE_SYSTEM_NIFTI)

# IF(NOT USE_SYSTEM_GIFTI)
#   SET(MINC2_deps ${MINC2_deps} GIFTI)
# ENDIF(NOT USE_SYSTEM_GIFTI)

IF(MINC2_deps)
  ADD_DEPENDENCIES(minc2 ${MINC2_deps})
ENDIF(MINC2_deps)

IF(MT_BUILD_SHARED_LIBS AND MINC2_deps)
  ADD_DEPENDENCIES(minc2_static  ${MINC2_deps})
ENDIF(MT_BUILD_SHARED_LIBS AND MINC2_deps)

# Setup main minc variables
SET(LIBMINC_USE_FILE ${CMAKE_BINARY_DIR}/libminc/UseLIBMINC.cmake CACHE INTERNAL "LIBMINC use file")
INCLUDE(${CMAKE_BINARY_DIR}/libminc/LIBMINCConfig.cmake)
INCLUDE(${LIBMINC_USE_FILE})

ADD_SUBDIRECTORY(minctools)

IF(NOT USE_SYSTEM_OPENJPEG)
  ADD_DEPENDENCIES(dcm2mnc  OPENJPEG)
ENDIF(NOT USE_SYSTEM_OPENJPEG)


ADD_SUBDIRECTORY(minc-widgets)
ADD_SUBDIRECTORY(EBTKS)
ADD_DEPENDENCIES(EBTKS minc2)

# setup EBTKS variables
SET(EBTKS_USE_FILE ${CMAKE_BINARY_DIR}/EBTKS/UseEBTKS.cmake CACHE INTERNAL "EBTKS use file")
INCLUDE(${CMAKE_BINARY_DIR}/EBTKS/EBTKSConfig.cmake)
INCLUDE(${EBTKS_USE_FILE})

ADD_SUBDIRECTORY(bicpl)
#setup bicpl variables
SET(BICPL_USE_FILE ${CMAKE_BINARY_DIR}/bicpl/UseBICPL.cmake CACHE INTERNAL "BICPL use file")
INCLUDE(${CMAKE_BINARY_DIR}/bicpl/BICPLConfig.cmake)
INCLUDE(${BICPL_USE_FILE})
ADD_DEPENDENCIES(bicpl minc2)

ADD_SUBDIRECTORY(arguments)

#setup arguments variables
SET(ARGUMENTS_USE_FILE ${CMAKE_BINARY_DIR}/bicpl/UseARGUMENTS.cmake CACHE INTERNAL "ARGUMENTS use file")
INCLUDE(${CMAKE_BINARY_DIR}/arguments/ARGUMENTSConfig.cmake)
INCLUDE(${ARGUMENTS_USE_FILE})

ADD_SUBDIRECTORY(oobicpl)
ADD_DEPENDENCIES(oobicpl minc2 bicpl arguments)

IF( NOT USE_SYSTEM_PCRE)
    ADD_DEPENDENCIES(oobicpl PCRE)
ENDIF( NOT USE_SYSTEM_PCRE)


#setup oobicpl variables
SET(OOBICPL_USE_FILE ${CMAKE_BINARY_DIR}/bicpl/UseOOBICPL.cmake CACHE INTERNAL "OOBICPL use file")
INCLUDE(${CMAKE_BINARY_DIR}/oobicpl/OOBICPLConfig.cmake)
INCLUDE(${OOBICPL_USE_FILE})

#ADD_SUBDIRECTORY(BEaST)
#ADD_SUBDIRECTORY(BEaST_library)
ADD_SUBDIRECTORY(conglomerate)
ADD_SUBDIRECTORY(classify)
ADD_SUBDIRECTORY(mni-perllib)
ADD_SUBDIRECTORY(N3)

ADD_SUBDIRECTORY(glim_image)
ADD_SUBDIRECTORY(mni_autoreg)

ADD_DEPENDENCIES(minctracc   LIBLBFGS)
ADD_DEPENDENCIES(mni_autoreg LIBLBFGS)
ADD_DEPENDENCIES(mni_autoreg mincresample)

ADD_SUBDIRECTORY(inormalize)
ADD_SUBDIRECTORY(mrisim)
ADD_SUBDIRECTORY(bic-pipelines)
ADD_SUBDIRECTORY(perl-modules)
ADD_SUBDIRECTORY(minc_gco)

IF(MT_BUILD_OPENBLAS)
  enable_language(Fortran)
  INCLUDE(BuildOpenBLAS)
  build_open_blas(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${MT_USE_OPENMP})
ENDIF(MT_BUILD_OPENBLAS)

#required for EZminc
IF(USE_SYSTEM_GSL)
  FIND_PACKAGE(GSL)
ELSE(USE_SYSTEM_GSL)
  INCLUDE(BuildGSL)
  build_gsl(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF(USE_SYSTEM_GSL)

IF(USE_SYSTEM_FFTW3F)
  INCLUDE(FindFFTW3F)
  FIND_PACKAGE(FFTW3F)
ELSE()
  INCLUDE(BuildFFTW3F)
  build_fftw3f(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF()

IF(USE_SYSTEM_FFTW3D)
  INCLUDE(FindFFTW3)
  FIND_PACKAGE(FFTW3)
ELSE(USE_SYSTEM_FFTW3D)
  INCLUDE(BuildFFTW3D)
  build_fftw3d(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ENDIF(USE_SYSTEM_FFTW3D)

# build tools that require ITK
IF(MT_BUILD_ITK_TOOLS)


  OPTION(MT_BUILD_ANTS     "Build ANTS " ON)
  OPTION(MT_BUILD_C3D      "Build convert3D " ON)
  OPTION(MT_BUILD_ELASTIX  "Build Elastix " ON)
  OPTION(MT_BUILD_ABC      "Build ABC " OFF)

  IF(USE_SYSTEM_ITK)
    FIND_PACKAGE(ITK)
  ELSE(USE_SYSTEM_ITK)
    OPTION(USE_SYSTEM_EXPAT_ITK  "Use System expat for ITKv4  "   OFF)

    IF(USE_SYSTEM_EXPAT_ITK)
    FIND_PACKAGE(EXPAT REQUIRED)
    ENDIF()

    INCLUDE(BuildITKv4)

    build_itkv4(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${CMAKE_BINARY_DIR}/libminc)

    # Setting dependecies
    ADD_DEPENDENCIES(ITKv4 minc2 )
    IF(NOT USE_SYSTEM_ZLIB)
        ADD_DEPENDENCIES(ITKv4 ZLIB)
    ENDIF()
    IF(NOT USE_SYSTEM_FFTW3D)
     ADD_DEPENDENCIES(ITKv4 FFTW3D)
    ENDIF()
    IF(NOT USE_SYSTEM_FFTW3F)
     ADD_DEPENDENCIES(ITKv4 FFTW3F)
    ENDIF()
  ENDIF(USE_SYSTEM_ITK)

  IF(MT_BUILD_ANTS)
   INCLUDE(BuildANTS)
   #INCLUDE(BuildBOOST)
   #build_BOOST(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
   build_ANTS(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ITK_DIR} ) #${BOOST_DIR}
   ADD_DEPENDENCIES(ANTS ITKv4 minc2 ) #BOOST
  ENDIF(MT_BUILD_ANTS)

  IF(MT_BUILD_C3D)
   INCLUDE(BuildC3D)
   build_C3D(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ITK_DIR})
   ADD_DEPENDENCIES(C3D ITKv4 minc2)
  ENDIF(MT_BUILD_C3D)
  
  IF(MT_BUILD_ELASTIX)
   INCLUDE(BuildElastix)
   build_Elastix(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ITK_DIR})
   ADD_DEPENDENCIES(Elastix ITKv4 minc2)
  ENDIF(MT_BUILD_ELASTIX)

  INCLUDE(BuildEZMINC)
  build_EZMINC(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${CMAKE_BINARY_DIR}/libminc ${CMAKE_BINARY_DIR}/bicpl ${ITK_DIR})
  ADD_DEPENDENCIES(EZMINC ITKv4 GSL minc2 minc_io bicpl)
  
  INCLUDE(BuildPatchMorphology)
  build_PatchMorphology(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ITK_DIR})
  ADD_DEPENDENCIES(patch_morphology ITKv4 minc_io)
  
  IF(MT_BUILD_OPENBLAS)
    ADD_DEPENDENCIES(patch_morphology OpenBLAS)
  ENDIF(MT_BUILD_OPENBLAS)
  
  IF(MT_BUILD_ABC)
   INCLUDE(BuildABC)
   build_ABC(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ITK_DIR})
   ADD_DEPENDENCIES(ABC ITKv4 minc2)
  ENDIF(MT_BUILD_ABC)
  
ENDIF(MT_BUILD_ITK_TOOLS)

# tools requiring OpenBLAS
INCLUDE(BuildBEAST)
build_BEAST(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
ADD_DEPENDENCIES(BEAST minc_io)

IF(MT_BUILD_OPENBLAS)
    ADD_DEPENDENCIES(BEAST OpenBLAS)
ELSE()
    FIND_PACKAGE(OpenBLAS )
ENDIF()


# build tools that require OpenGL and GLUT
IF(MT_BUILD_VISUAL_TOOLS)
  FIND_PACKAGE( OpenGL REQUIRED )
  FIND_PACKAGE( X11    QUIET )
  
  IF(USE_SYSTEM_GLUT)
    FIND_PACKAGE( GLUT   REQUIRED ) 
  ELSE(USE_SYSTEM_GLUT)
    INCLUDE(BuildFREEGLUT) # newer version of build_freeglut seem to be causing problems
    build_freeglut( ${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
  ENDIF(USE_SYSTEM_GLUT)

  ADD_SUBDIRECTORY(bicgl)
  INCLUDE(${CMAKE_BINARY_DIR}/bicgl/BICGLConfig.cmake)

  ADD_SUBDIRECTORY(ray_trace)
  ADD_SUBDIRECTORY(Display)
  ADD_SUBDIRECTORY(Register)
  ADD_SUBDIRECTORY(ILT)

  IF(NOT USE_SYSTEM_GLUT)
    ADD_DEPENDENCIES(register FREEGLUT)
    ADD_DEPENDENCIES(Display FREEGLUT)
  ENDIF(NOT USE_SYSTEM_GLUT)

  IF(MT_BUILD_SHARED_LIBS)
    ADD_DEPENDENCIES(ray_trace bicgl minc2_static bicpl_static)
    ADD_DEPENDENCIES(Display   bicgl minc2_static bicpl_static)
    ADD_DEPENDENCIES(register  bicgl minc2_static bicpl_static)
  ELSE(MT_BUILD_SHARED_LIBS)
    ADD_DEPENDENCIES(ray_trace bicgl minc2 bicpl  )
    ADD_DEPENDENCIES(Display   bicgl minc2 bicpl  )
    ADD_DEPENDENCIES(register  bicgl minc2 bicpl  )
  ENDIF(MT_BUILD_SHARED_LIBS)

  IF(X11_FOUND)
    ADD_SUBDIRECTORY(postf)
    ADD_DEPENDENCIES(postf minc2)
    ADD_SUBDIRECTORY(xdisp)
    ADD_DEPENDENCIES(xdisp minc2)
  ENDIF(X11_FOUND)
ENDIF(MT_BUILD_VISUAL_TOOLS)

IF(MT_BUILD_IM)
    message("Building minimal imagemagick, use with caution!")
    INCLUDE(BuildJPEG)
    INCLUDE(BuildPNG)
    INCLUDE(BuildFREETYPE)
    INCLUDE(BuildIM)
        
    build_jpeg(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX})
    build_png(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARY} ${ZLIB_DIR} )
    build_freetype(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARY} ${ZLIB_DIR})
    build_im(${CMAKE_INSTALL_PREFIX} ${SUPERBUILD_STAGING_PREFIX} ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARY} ${ZLIB_DIR})
    ADD_DEPENDENCIES(FREETYPE PNG)
    ADD_DEPENDENCIES(IM FREETYPE PNG JPEG)
ENDIF(MT_BUILD_IM)

  #install external dependencies to final destination
INSTALL(DIRECTORY ${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/bin
        DESTINATION .
        PATTERN *
        PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE )

INSTALL(DIRECTORY ${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}
        DESTINATION .)

INSTALL(DIRECTORY ${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/include
        DESTINATION .)

# share can be created by some packages
INSTALL(DIRECTORY ${SUPERBUILD_STAGING_PREFIX}/${CMAKE_INSTALL_PREFIX}/share
        DESTINATION .)


ADD_SUBDIRECTORY(man-pages)


# write configuration information
IF(UNIX)
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/minc-toolkit-config.unix.sh.cmake ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.sh @ONLY)
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/minc-toolkit-config.unix.csh.cmake ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.csh @ONLY)
ENDIF(UNIX)

IF(APPLE)
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/minc-toolkit-config.apple.sh.cmake ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.sh @ONLY)
  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/minc-toolkit-config.apple.csh.cmake ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.csh @ONLY)
ENDIF(APPLE)

INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.sh ${CMAKE_CURRENT_BINARY_DIR}/minc-toolkit-config.csh DESTINATION .)

# setup additional variables for making Debian package
INCLUDE(DebianPackageAddons)

# setup additional variables for making RPM package
INCLUDE(RPMPackageAddons)


# according to http://www.cmake.org/pipermail/cmake/2011-November/047137.html it's better to put this in the end
INCLUDE(CPack)
